package com.miqtech.master.wyrecorder.utils;

import com.miqtech.master.wyrecorder.WYRecorderApp;
import com.miqtech.master.wyrecorder.entity.LiveTypeInfo;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
 * Created by admin on 2016/12/23.
 */

public class FileUtils {
    private final static String CACHE_DIR = WYRecorderApp.getContext().getCacheDir().getAbsolutePath();

    public static void writeData(ArrayList<LiveTypeInfo> datas) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream(CACHE_DIR + "/GAME_CACHE");
            L.e("FileUtils", CACHE_DIR + datas.size());
            if (fos != null) {
                oos = new ObjectOutputStream(fos);
                if (oos != null) {
                    oos.writeObject(datas);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            CloseUtil.close(oos);
            CloseUtil.close(fos);
        }
    }

    public static ArrayList<LiveTypeInfo> readData() {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(CACHE_DIR + "/GAME_CACHE");
            if (fis != null) {
                ois = new ObjectInputStream(fis);
                if (ois != null) {
                    return (ArrayList<LiveTypeInfo>) ois.readObject();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            CloseUtil.close(ois);
            CloseUtil.close(fis);
        }
        return null;
    }
}
